package com.study.controller;

import com.study.domain.MyUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * Description
 *
 * @ClassName com.study.controller
 * @Copyright 炫彩互动
 * @Author yanglh
 * @Create Date  2016/4/29
 * @Modified by none
 * @Modified Date
 */

public class BaseController {
    public long getCurrentUserId() {
        long id = 0L;
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            return id;
        }
        Authentication authentication = context.getAuthentication();
        if (authentication != null) {
            Object principal = authentication.getPrincipal();
            if (principal instanceof MyUser) {
                id = ((MyUser) principal).getId();
            }
        }
        return id;
    }
}
